package com.smart.beanfactory;

import com.smart.Car;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;

/**
 * @author chenxingyu
 * @date 2019/8/26 20:42
 */
public class MyBeanPostProcessor implements BeanPostProcessor {

    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if ("car".equals(beanName)) {
            Car car = (Car) bean;
            if (car.getColor() == null) {
                System.out.println("调用MyBeanPostProcessor.postProcessBeforeInitialization()，color为空，设置为默认黑色");
                car.setColor("黑色");
            }

        }
        return bean;
    }

    @Bean
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if ("car".equals(beanName)) {
            if (beanName.equals("car")) {
                Car car = (Car) bean;
                if (car.getMaxSpeed() >= 200) {
                    System.out.println("调用MyBeanPostProcessor.postProcessAfterInitialization()，将maxSpeed调整为200");
                    car.setMaxSpeed(200);
                }
            }
        }
        return bean;
    }
}
